package com.ruoyi.web.sso.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.StaffDetailsUtil;
import com.ruoyi.web.sso.common.util.UuidUtil;
import com.ruoyi.web.sso.domain.Group;
import com.ruoyi.web.sso.domain.Menu;
import com.ruoyi.web.sso.domain.RoleMenu;
import com.ruoyi.web.sso.domain.vo.AddMenu;
import com.ruoyi.web.sso.domain.vo.StaffDetails;
import com.ruoyi.web.sso.domain.vo.ZtreeSso;
import com.ruoyi.web.sso.service.IMenuService;
import com.ruoyi.web.sso.util.StringUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-10-20
 */
@Controller
@RequestMapping("/sso/menu")
@Api(tags = "菜单信息")
public class MenuController  extends BaseController {

    @Autowired
    private IMenuService menuService;


    @Autowired
    private ISysMenuService sysMenuService;

    @RequestMapping(value="/add",method= RequestMethod.POST)
    @ApiOperation(value="新增菜单",notes="新增菜单")
    @ResponseBody
    public AjaxResult add(@Validated AddMenu addMenu)
    {
        return menuService.add(addMenu);
    }


    @PostMapping(value="/platformIdByList")
    @ApiOperation(value="查询指定系统菜单列表",notes="查询指定系统菜单列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Menu.class),
    })
    @ResponseBody
    public TableDataInfo platformIdByList(@ApiParam(value = "系统ID") @RequestParam String platformId)
    {
        startPage();
        Menu menu = new Menu();
        menu.setPlatformId(platformId);
        List<Menu> list = menuService.selectMenuList(menu);
        return getDataTable(list);
    }


    @PostMapping(value="/addRoleMenu")
    @ApiOperation(value="新增角色菜单",notes="新增角色菜单")
    @ResponseBody
    public AjaxResult addRoleMenu(@RequestBody RoleMenu sosRoleMenu)
    {
        return menuService.addRoleMenu(sosRoleMenu);
    }


    @PostMapping(value="/selectRoleMenu")
    @ApiOperation(value="查询角色菜单",notes="查询角色菜单")
    @ResponseBody
    public AjaxResult selectRoleMenu(HttpServletRequest request, @ApiParam(value = "系统ID") @RequestParam String platformId,
                                     @ApiParam(value = "角色ID") @RequestParam String roleId )
    {
        return menuService.selectRoleMenu(request,platformId,roleId);
    }




    @PostMapping("/list")
    @ApiOperation(value="后台管理菜单列表",notes="后台管理菜单列表")
    @ResponseBody
    public  List<Menu>  list(@ApiParam(value = "菜单名字")@RequestParam(required=false) String menuName,
                             @ApiParam(value = "系统ID")@RequestParam() String platformId)
    {
        Menu menu = new Menu();
        menu.setPlatformId(platformId);
        if (AssertUtil.isNotEmpty(menuName)){
            menu.setMenuName(menuName);
        }
        return menuService.selectMenuList(menu);
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    @ApiOperation(value="加载角色菜单列表树",notes="加载角色菜单列表树")
    public List<ZtreeSso> roleMenuTreeData(@ApiParam(value = "系统ID")@RequestParam() String platformId,String roleId)
    {
        List<ZtreeSso> ztrees = menuService.roleMenuTreeData(roleId, platformId);
        return ztrees;
    }


    @RequestMapping(value="/updateMenu",method= RequestMethod.POST)
    @ApiOperation(value="修改菜单",notes="修改菜单")
    @ResponseBody
    public AjaxResult addPlatformIdMenu(@Validated Menu Menu)
    {
        return toAjax(menuService.updateMenu(Menu));
    }

    /**
     * 删除菜单
     */
    @GetMapping("/remove/{menuId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("menuId") String menuId)
    {
        return menuService.deleteMenuById(menuId);
    }



    @RequestMapping(value="/copy",method= RequestMethod.POST)
    @ApiOperation(value="拷贝菜单",notes="拷贝菜单")
    @ResponseBody
    public void copy(@ApiParam(value = "系统ID") @RequestParam String platformId,String oldPlatformId)
    {
        Menu menu = new Menu();
        menu.setPlatformId(oldPlatformId);
        List<Menu> list = menuService.selectMenuList(menu);
        for (int i = 0; i < list.size(); i++) {
            Menu m = list.get(i);
            if (StringUtils.equals(m.getParentId() ,"0")){
                String menuId = m.getMenuId();
                String newMenuId = UuidUtil.getUuid();
                m.setMenuId(newMenuId);
                m.setPlatformId(platformId);
                m.setCreateTime(new Date());
                menuService.insertMenu(m);
                copyMenu(platformId,oldPlatformId,menuId,newMenuId);
            }

        }
    }
    public void copyMenu(String platformId,String oldPlatformId,String menuId,String newMenuId){
        Menu menu = new Menu();
        menu.setParentId(menuId);
        menu.setPlatformId(oldPlatformId);
        List<Menu> list = menuService.selectMenuList(menu);
        for (int i = 0; i < list.size(); i++) {
            Menu m = list.get(i);
            if (!StringUtils.equals(m.getParentId() ,"0")){
                String menuId_1 = m.getMenuId();
                String newMenuId_1 = UuidUtil.getUuid();
                m.setMenuId(newMenuId_1);
                m.setPlatformId(platformId);
                m.setCreateTime(new Date());
                m.setParentId(newMenuId);
                menuService.insertMenu(m);
                copyMenu(platformId,oldPlatformId,menuId_1,newMenuId_1);
            }

        }
    }
}
